document.addEventListener('keyup', keyboardNavigation, false); 
window.addEventListener('load',scanLink,false);
function keyboardNavigation(e) 
{
  if (!e.altKey && e.shiftKey && e.ctrlKey) 
  { 
    if (e.keyCode==37)
    {
      chrome.extension.sendRequest(-1);
    } else if (e.keyCode==39)
    {
      chrome.extension.sendRequest(1);
    } else if (e.keyCode=79)
    {
      scanLink();
    }
    //alert(e.keyCode);
  }
}

var next=false;
var prev=false;
var unscanned=true;

function scanGoogle()
{
  var gnext=document.getElementById("pnnext");
  if (gnext!=null) next=gnext;
  var gprev=document.getElementById("pnprev");
  if (gprev!=null) prev=gprev;
/*
  div=document.getElementById("navcnt");
  if (div!=null)
  {
    var tds = document.getElementById("navcnt").getElementsByTagName('td');
    if (tds.length>0)
    {
      if (tds[0].getAttribute("class")=="b") links=tds[0]. getElementsByTagName('a');
      if (links.length>0) prev=links[0];
      var last=tds.length-1;
      if (tds[last].getAttribute("class")=="b") links=tds[last]. getElementsByTagName('a');
      if (links.length>0) next=links[0];
    }
  }
*/
}

function scanLink()
{
  next=false;
  prev=false;
  unscanned=false;
  var links;
  var specialUrl=false;
  if (window.location.pathname.search("/search")==0)
  {
    var url=window.location.hostname;
    var div;
    if (url.search("www.google.")>=0)
    {
      specialUrl=true;
      scanGoogle();
    } else if (url.search("www.bing.")>=0)
    {
      specialUrl=true;
      var divs=document.getElementsByTagName("div");
      div=null;
      var int=0;
      while (div==null && int<divs.length)
      {
        if (divs[int].getAttribute("class")=="sb_pag") div=divs[int];
        int++;
      } 
      if (div!=null)
      {
        links = div.getElementsByTagName('a');
        for (i=0; i<links.length; i++) 
          if (links[i].getAttribute("class")=="sb_pagN")next=links[i];
          else if (links[i].getAttribute("class")=="sb_pagP")prev=links[i];
      }
    } else if (url.search("search.yahoo.")>=0)
    {
      specialUrl=true;
      div=document.getElementById("pg");
      if (div!=null)
      {
        links = div.getElementsByTagName('a');
        for (i=0; i<links.length; i++) 
          if (links[i].id=="pg-next")next=links[i];
          else if (links[i].id=="pg-prev")prev=links[i];
      }
    }
  }
  var bloggerPager=document.getElementById('blog-pager');
  if (!specialUrl && bloggerPager)
  {
    links = bloggerPager.getElementsByTagName('a');
    for (i=0; i<links.length; i++) 
    {
      if (links[i].getAttribute('href')!="")
      {
        if (links[i].getAttribute('class')=="blog-pager-older-link")next=links[i];
        if (links[i].getAttribute('class')=="blog-pager-newer-link")prev=links[i];
      }
    }
    if (next||prev)specialUrl=true;
  }
  if (!specialUrl)
  {
    links = document.getElementsByTagName('a');
    for (i=0; i<links.length; i++) 
    {
      var useLink=0;
      if (links[i].getAttribute('href')!="")
      {
        if (links[i].getAttribute("rel")=="next") useLink=1;
        else if (links[i].getAttribute("rel")=="prev") useLink=-1;
        else {
          var ltext=links[i].innerText.toLowerCase();
          if (ltext.length<12 && ltext.match("^next")=="next") useLink=1;
          else if (ltext.length<12 && ltext.match("prev$")=="prev") useLink=-1;
          else if (ltext.length<16 && ltext.match("previous$")=="previous") useLink=-1;
          else if (ltext.length<16 && ltext.match("previous page$")=="previous page") useLink=-1;
        }
        if (useLink==0 || links[i].getAttribute('href')==""){}
        else if (useLink== 1)next=links[i];
        else if (useLink==-1)prev=links[i];
      }
    }
  }
  
}

var img = new Image();
img.src = chrome.extension.getURL("arrowanim.png");
img.style.position="relative";
img.style.borderTopRightRadius= "10px";
img.style.borderBottomRightRadius= "10px";

chrome.extension.onRequest.addListener
(
  function(request, sender, sendResponse) 
  {
    if (request.command=="geturls")
    {
      if (unscanned && request.force)
      {
        scanLink();
      }else if ((window.location.hostname.search("www.google.")>=0) && 
                (window.location.pathname.search("/search")==0 || 
                 window.location.pathname.search("/webhp")==0 ||
                 window.location.pathname.search("/")==0))
        scanGoogle();
      sendResponse({next: (next?next.href:""),prev: (prev?prev.href:"")});
    } if (request.command=="animnext")
    {
      var arrDiv=document.createElement('div');
      document.body.appendChild(arrDiv);
      arrDiv.setAttribute('id',"arrowUrlNeXT");
      arrDiv.style.position='fixed';
      factor=125;
      basepoint=request.from<=0?0:(window.innerWidth-factor);
      arrDiv.style.top=(window.innerHeight/3)+"px";
      arrDiv.style.zIndex=1000;
      img.style.background=request.background;
      arrDiv.style.webkitTransitionProperty='left,opacity';
      arrDiv.style.webkitTransitionDuration='0.3s,1.4s';
      arrDiv.style.webkitTransitionTimingFunction='ease-out,ease-in';
      arrDiv.style.left=(request.from*factor+basepoint)+"px";
      arrDiv.appendChild(img);

      if (request.from>request.to)arrDiv.style.webkitTransform="scaleX(-1)";
      
      setTimeout('document.getElementById("arrowUrlNeXT").style.left="'+(request.to*factor+basepoint)+'px";document.getElementById("arrowUrlNeXT").style.opacity="0"',1);
      setTimeout('document.body.removeChild(document.getElementById("arrowUrlNeXT"))',1500);
    }
  }
);
